package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Employee;
import com.woniu.supermarket.mapper.EmployeeMapper;
import com.woniu.supermarket.service.IEmployeeService;
import com.woniu.supermarket.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public Employee login(String username) {
        return employeeMapper.selectByName(username);
    }
//    分页查询
    @Override
    public PageInfo<EmployeeVo> findEmployeeByCons(Integer now, Integer size, String employeeNumber, Integer status) {
        PageHelper.startPage(now,size);
        List<EmployeeVo> employeeList = employeeMapper.selectByCons(employeeNumber,status);
        /*System.out.println("==================================分页===============");
        for(EmployeeVo e : employeeList){
            System.out.println(e);
        }*/
        return new PageInfo<>(employeeList);
    }
//添加员工
    @Override
    @Transactional
    public int addEmployee(EmployeeVo employeeVo) {
        //1.添加员工相关信息
        int i = employeeMapper.insert(employeeVo);
        //2.给当前添加的员工赋予角色
        //employeeVo.getId():添加的员工id
        //employeeVo.getRoleIds()：员工的角色id
        employeeMapper.insertRoleForEmployee(employeeVo.getId(),employeeVo.getRolesIds());
        return i;
    }
//修改员工
    @Override
    @Transactional
    public int modifyEmployee(EmployeeVo employeeVo) {
        //1.删除当前员工绑定的角色
        employeeMapper.deleteEmpRoleByEmpId(employeeVo.getId());
        //2.重新给当前员工赋值角色
        employeeMapper.insertRoleForEmployee(employeeVo.getId(),employeeVo.getRolesIds());
        //3.修改员工信息
        return employeeMapper.update(employeeVo);
    }
    //删除员工
    @Override
    @Transactional
    public int removeEmployee(Integer id) {
        //1.解除员工与角色的绑定
        employeeMapper.deleteEmployeeRoleById(id);
        //删除员工
        return employeeMapper.deleteByPrimaryKey(id);
    }
    //批量删除
    @Override
    public int removeBatchIds(int[] ids) {
        return employeeMapper.deleteBatchEmp(ids);
    }
    //根据id查询员工
    @Override
    public Employee queryEmployeeByKey(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}
